package com.mengshu.platform.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mengshu.mybatisplus.basic.BasicServiceImpl;
import com.mengshu.platform.mapper.AdminUserMapper;
import com.mengshu.platform.po.AdminUserPO;
import com.mengshu.platform.service.AdminUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service
public class AdminUserServiceImpl extends BasicServiceImpl<AdminUserMapper, AdminUserPO> implements AdminUserService {

    @Override
    public AdminUserPO getByName(String name) {
        return getOne(Wrappers.lambdaQuery(AdminUserPO.class).eq(p -> p.getName(), name));
//        return getOne(Wrappers.lambdaQuery(AdminUserPO.class).eq(AdminUserPO::getName, name));
    }

    @Override
    public Set<String> getPermissions(long userId) {
        return getBaseMapper().getPermissions(userId);
    }

    @Override
    public Set<String> listPermissions() {
        return getBaseMapper().listPermissions();
    }

    @Override
    @Transactional
    public boolean updateForUniqueKey(AdminUserPO user) {
        final AdminUserPO target = getById(user.getId());
        if (target == null) {
            return false;
        }
        user.setPassword(target.getPassword());
        user.setSalt(target.getSalt());
        return super.updateForUniqueKey(user);
    }
}
